package com.joolun.weixin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.common.corp.TenantContext;
import com.joolun.common.utils.DateUtils;
import com.joolun.system.domain.ScrmCustomerInfo;
import com.joolun.weixin.entity.ScrmChannelRecordLog;
import com.joolun.weixin.entity.vo.ScrmChannelRecordLogVo;
import com.joolun.weixin.mapper.ScrmChannelRecordLogMapper;
import com.joolun.weixin.service.IScrmChannelRecordLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 渠道数据日志Service业务层处理
 * 
 * @author joolun
 * @date 2025-04-11
 */
@Service
public class ScrmChannelRecordLogServiceImpl extends  ServiceImpl<ScrmChannelRecordLogMapper, ScrmChannelRecordLog> implements IScrmChannelRecordLogService
{
    @Autowired
    private ScrmChannelRecordLogMapper scrmChannelRecordLogMapper;

    /**
     * 查询渠道数据日志
     * 
     * @param id 渠道数据日志主键
     * @return 渠道数据日志
     */
    @Override
    public ScrmChannelRecordLog selectScrmChannelRecordLogById(Long id)
    {
        return scrmChannelRecordLogMapper.selectScrmChannelRecordLogById(id);
    }

    /**
     * 查询渠道数据日志列表
     * 
     * @param scrmChannelRecordLog 渠道数据日志
     * @return 渠道数据日志
     */
    @Override
    public List<ScrmChannelRecordLog> selectScrmChannelRecordLogList(ScrmChannelRecordLog scrmChannelRecordLog)
    {
        return scrmChannelRecordLogMapper.selectScrmChannelRecordLogList(scrmChannelRecordLog);
    }

    /**
     * 折线图
     *
     * @param params 渠道数据日志
     * @return 渠道数据日志集合
     */
    public List<ScrmChannelRecordLogVo> selectChart(Map<String, Object> params){
        // 添加主体id
        params.put("corpId", TenantContext.getTenantId());
        return scrmChannelRecordLogMapper.selectChart(params);
    }
    /**
     * 新增渠道数据日志
     * 
     * @param scrmChannelRecordLog 渠道数据日志
     * @return 结果
     */
    @Override
    public int insertScrmChannelRecordLog(ScrmChannelRecordLog scrmChannelRecordLog)
    {
        scrmChannelRecordLog.setExtCorpId(TenantContext.getTenantId());
        scrmChannelRecordLog.setCreateTime(DateUtils.getNowDate());
        return scrmChannelRecordLogMapper.insertScrmChannelRecordLog(scrmChannelRecordLog);
    }

    /**
     * 修改渠道数据日志
     * 
     * @param scrmChannelRecordLog 渠道数据日志
     * @return 结果
     */
    @Override
    public int updateScrmChannelRecordLog(ScrmChannelRecordLog scrmChannelRecordLog)
    {
        scrmChannelRecordLog.setUpdateTime(DateUtils.getNowDate());
        return scrmChannelRecordLogMapper.updateScrmChannelRecordLog(scrmChannelRecordLog);
    }

    /**
     * 批量删除渠道数据日志
     * 
     * @param ids 需要删除的渠道数据日志主键
     * @return 结果
     */
    @Override
    public int deleteScrmChannelRecordLogByIds(Long[] ids)
    {
        return scrmChannelRecordLogMapper.deleteScrmChannelRecordLogByIds(ids);
    }

    /**
     * 删除渠道数据日志信息
     * 
     * @param id 渠道数据日志主键
     * @return 结果
     */
    @Override
    public int deleteScrmChannelRecordLogById(Long id)
    {
        return scrmChannelRecordLogMapper.deleteScrmChannelRecordLogById(id);
    }

    @Override
    public int add(Long planId,String extCustomerId,String extCorpId,String type){
        ScrmChannelRecordLog scrmChannelRecordLog = new ScrmChannelRecordLog();
        scrmChannelRecordLog.setAddDate(DateUtils.getYMD());
        scrmChannelRecordLog.setAddTime(new Date());
        scrmChannelRecordLog.setType(type);
        scrmChannelRecordLog.setExtCorpId(extCorpId);
        scrmChannelRecordLog.setPlanId(planId);
        scrmChannelRecordLog.setExtCustomerId(extCustomerId);
        save(scrmChannelRecordLog);
        return 1;
    }


    @Override
    public int changeFriend(ScrmCustomerInfo scrmCustomerInfo, String type){
        Long planId = scrmCustomerInfo.getPlanId();
        String extCustomerId = scrmCustomerInfo.getExtCustomerId();
        String extCorpId = scrmCustomerInfo.getExtCorpId();

        ScrmChannelRecordLog scrmChannelRecordLog = new ScrmChannelRecordLog();
        scrmChannelRecordLog.setFirstDayDelFriend(0);
        Date createdAt = scrmCustomerInfo.getCreatedAt();
        // 判断删友时间是否当天设置首日删友字段值
        if(type.equals("DelFriend")){
            if(DateUtils.checkToday(createdAt)){
                scrmChannelRecordLog.setFirstDayDelFriend(1);
            }
        }
        if(type.equals("JV")){
            scrmChannelRecordLog.setGender(scrmCustomerInfo.getGender());
        }
        scrmChannelRecordLog.setAddDate(DateUtils.getYMD());
        scrmChannelRecordLog.setAddTime(new Date());
        scrmChannelRecordLog.setType(type);
        scrmChannelRecordLog.setExtCorpId(extCorpId);
        scrmChannelRecordLog.setPlanId(planId);
        scrmChannelRecordLog.setExtCustomerId(extCustomerId);
        save(scrmChannelRecordLog);
        return 1;
    }


}
